package com.fh.controller.penalty_interest_reduction;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fh.annotation.SysLog;
import com.fh.controller.base.BaseController;
import com.fh.job.PaopiActionJob;
import com.fh.mapper.loan.CallLoanBorrowInfoMapper;
import com.fh.mapper.loan.ReducteOverdueInfoMapper;
import com.fh.model.Page;
import com.fh.model.loan.CallLoanBorrowInfo;
import com.fh.model.loan.CallLoanInfo;
import com.fh.model.loan.ReducteOverdueInfo;
import com.fh.service.penalty_interest_reduction.IPenaltyInterestReductionService;
import com.fh.service.penalty_interest_reduction.IReducteOverdueInfoService;
import com.fh.service.penalty_interest_reduction.ReducteOverdueInfoServiceImpl;
import com.fh.service.system.user.UserService;
import com.fh.util.Const;
import com.fh.util.CoverUtil;
import com.fh.util.DateUtil;
import com.fh.util.DelAllFile;
import com.fh.util.CoverUtil;
import com.fh.util.PageData;
import com.fh.util.ResultJSON;
import com.fh.util.UserUtils;
import com.github.ltsopensource.core.json.JSON;
import com.ybd.common.mask.MaskTypeEnum;
/**
 * 
 * @author yc、
 *
 */
@Controller
@RequestMapping("faxijianmian")
public class PenaltyInterestReductionController extends BaseController{
	@Autowired
	private IPenaltyInterestReductionService PIRservice;
	@Autowired
	private IReducteOverdueInfoService ROIService;
	@Autowired
	private ReducteOverdueInfoServiceImpl reducteOverdueInfoServiceImpl;
	@Autowired
	private UserService userService;
	
	private Logger loger = LoggerFactory.getLogger(PenaltyInterestReductionController.class);
	/**
	 * 罚息查询列表
	 * 
	 * 
	 * @param page
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("faxichaxun")
	public ModelAndView indexList(Page page) {
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		
		//================高级查询===================
		String CALLLOANNID = pd.getString("callLoanNid");
		if(null != CALLLOANNID && !"".equals(CALLLOANNID)){
			CALLLOANNID = CALLLOANNID.trim();
			pd.put("CALLLOANNID", CALLLOANNID);
		}
		String REPAYMENTID = pd.getString("repaymentId");
		if(null != REPAYMENTID && !"".equals(REPAYMENTID)){
			REPAYMENTID = REPAYMENTID.trim();
			pd.put("REPAYMENTID", REPAYMENTID);
		}
		String REALNAME = pd.getString("realName");
		if(null != REALNAME && !"".equals(REALNAME)){
			REALNAME = REALNAME.trim();
			pd.put("REALNAME", REALNAME);
		}
		String CONTIDCARD = pd.getString("contIdCard");
		if(null != CONTIDCARD && !"".equals(CONTIDCARD)){
			CONTIDCARD = CONTIDCARD.trim();
			pd.put("CONTIDCARD", CONTIDCARD);
		}
		//================高级查询===================
		pd.put("status", 1);
		page.setPd(UserUtils.setDataRole(pd,userService));//获取页面参数
		page.setPd(pd);
		
		List<PageData>	list = PIRservice.findlistPage(page);
		Map<String,MaskTypeEnum> map = new HashMap<>();
        map.put("contIdCard",MaskTypeEnum.IDCARD2);
		CoverUtil.setListCover(list, map);
		mv.setViewName("/penaltyInterestReduction/penaltyInterestReductionList");
		mv.addObject("pageList", list);
		mv.addObject("pd", pd);
		//mv.addObject(Const.SESSION_QX,1);	//按钮权限
		return mv;
		
	}
	/**
	 * 罚息减免申请视图
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("faxijianmianApply")
	@ResponseBody
	public ModelAndView faxijianmianApply(String callLoanNid,Model model){
		ModelAndView mv = this.getModelAndView();
		CallLoanInfo info = PIRservice.faxijianmianApplyById(callLoanNid);
		info.setContIdCard(CoverUtil.IdCard2(info.getContIdCard()));
		//剩余可申请的罚息减免金额
		BigDecimal receiveMoney = reducteOverdueInfoServiceImpl.getReceiveMoney(info);
		mv.addObject("reduDay", DateUtil.getTime2());
		mv.addObject("info", info);
		mv.addObject("receiveMoney", receiveMoney);
		mv.setViewName("/penaltyInterestReduction/penaltyInterestReductionApply");
		return mv;
	}
	/**
	 * 罚息退回申请视图
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("faxituihuiApply")
	@ResponseBody
	public ModelAndView faxituihuiApply(String callLoanNid,Model model){
		ModelAndView mv = this.getModelAndView();
		CallLoanInfo info = PIRservice.faxijianmianApplyById(callLoanNid);
		info.setContIdCard(CoverUtil.IdCard2(info.getContIdCard()));
		//剩余可申请的罚息退免金额
		BigDecimal returnMoney = reducteOverdueInfoServiceImpl.getReturnMoney(info);
		mv.addObject("info", info);
		mv.addObject("returnMoney", returnMoney);
		mv.setViewName("/penaltyInterestReduction/penaltyInterestReturnApply");
		return mv;
	}
	/**
	 * 罚息减免申请
	 * 
	 * @param info
	 * @return
	 */
	@SysLog("罚息减免申请")
	@RequestMapping("faxijianmianApplyAudit")
	@ResponseBody
	public ResultJSON faxijianmianApplyAudit(ReducteOverdueInfo info){
		ResultJSON json = new ResultJSON();
		loger.info("罚息减免申请:PenaltyInterestReductionController faxijianmianApplyAudit:" + JSON.toJSONString(info));
		try {
			ROIService.insert(info);
			json.setMsg("申请成功!");
		} catch (Exception e) {
			loger.error("罚息减免申请失败:PenaltyInterestReductionController faxijianmianApplyAudit:" + e);
			json.setSuccess(false);
			json.setMsg(e.getMessage());
		}
		return json;
	}
	/**
	 * 罚息退回申请
	 * 
	 * @param info
	 * @return
	 */
	@SysLog("罚息退回申请")
	@RequestMapping("faxituihuiApplyAudit")
	@ResponseBody
	public ResultJSON faxituihuiApplyAudit(ReducteOverdueInfo info){
		loger.info("罚息退回申请:PenaltyInterestReductionController faxituihuiApplyAudit:" + JSON.toJSONString(info));
		ResultJSON json = new ResultJSON();
		try {
			ROIService.insert2(info);
			json.setMsg("申请成功!");
		} catch (Exception e) {
			loger.error("罚息退回申请失败:PenaltyInterestReductionController faxituihuiApplyAudit:" + e);
			json.setSuccess(false);
			json.setMsg(e.getMessage());
		}
		return json;
	}
	/**
	 * 罚息减免待审核列表
	 * 
	 * @param page
	 * @return
	 */
	@RequestMapping("faxijianmianAuditList")
	public ModelAndView AuditList(Page page){
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		
		//================高级查询===================
		String REDUAPPLYID = pd.getString("reduApplyId");
		if(null != REDUAPPLYID && !"".equals(REDUAPPLYID)){
			REDUAPPLYID = REDUAPPLYID.trim();
			pd.put("REDUAPPLYID", REDUAPPLYID);
		}
		String CALLLOANNID = pd.getString("callLoanNid");
		if(null != CALLLOANNID && !"".equals(CALLLOANNID)){
			CALLLOANNID = CALLLOANNID.trim();
			pd.put("CALLLOANNID", CALLLOANNID);
		}
		
		String REPAYMENTID = pd.getString("repaymentId");
		if(null != REPAYMENTID && !"".equals(REPAYMENTID)){
			REPAYMENTID = REPAYMENTID.trim();
			pd.put("REPAYMENTID", REPAYMENTID);
		}
		String REALNAME = pd.getString("realName");
		if(null != REALNAME && !"".equals(REALNAME)){
			REALNAME = REALNAME.trim();
			pd.put("REALNAME", REALNAME);
		}
		String CONTIDCARD = pd.getString("contIdCard");
		if(null != CONTIDCARD && !"".equals(CONTIDCARD)){
			CONTIDCARD = CONTIDCARD.trim();
			pd.put("CONTIDCARD", CONTIDCARD);
		}
		//================高级查询===================
		pd.put("status", "0");//待审核状态
		page.setPd(UserUtils.setDataRole(pd,userService));//获取页面参数
		page.setPd(pd);
		//获取审核权限
		Map<String, String> map = UserUtils.getCurrentRole();
		Integer QX2 = Integer.valueOf(map.get(Const.SESSION_QX2));
		
		List<PageData>	list = ROIService.findlistPage(page);
		Map<String,MaskTypeEnum> map3 = new HashMap<>();
        map3.put("contIdCard",MaskTypeEnum.IDCARD2);
		CoverUtil.setListCover(list, map3);
		mv.setViewName("/penaltyInterestReduction/faxijianmianAuditList");
		mv.addObject("pageList", list);
		mv.addObject("pd", pd);
		mv.addObject(Const.SESSION_QX2,QX2);
		return mv;
	}
	/**
	 * 单笔审核页面
	 * 
	 * @return
	 */
	@RequestMapping("auditOne")
	public ModelAndView auditOneList(String reduApplyId){
		ModelAndView mv = this.getModelAndView();
		PageData info = ROIService.queryByReduApplyId(reduApplyId);
		info.put("contIdCard", CoverUtil.IdCard2(info.get("contIdCard").toString()));
		mv.setViewName("/penaltyInterestReduction/auditOneList");
		mv.addObject("info", info);
		return mv;
	}
	/**
	 * 提交审核
	 * 
	 * @return
	 */
	@SysLog("罚息退免单笔审核")
	@RequestMapping("auditOneSave")
	@ResponseBody
	public ResultJSON auditOneSave(ReducteOverdueInfo info){
		loger.info("罚息退免单笔审核:PenaltyInterestReductionController auditOneSave:" + JSON.toJSONString(info));
		ResultJSON json = new ResultJSON();
		try {
			ROIService.auditOne(info);
		} catch (Exception e) {
			loger.error("罚息退免单笔审核:PenaltyInterestReductionController auditOneSave:",e);
			json.setSuccess(false);
			json.setMsg(e.getMessage());
			
		}
		return json;
	}
	/**
	 * 批量审核页面
	 */
	@RequestMapping("auditBatch")
	@ResponseBody
	public ModelAndView auditBatch(String DATA_IDS) {
		ModelAndView mv = this.getModelAndView();
		mv.setViewName("/penaltyInterestReduction/auditBatchList");
		//Map<String,Object> map = new HashMap<String,Object>();
		try {
			//List<PageData> pdList = new ArrayList<PageData>();
			List<PageData> pathList = new ArrayList<PageData>();
			if(null != DATA_IDS && !"".equals(DATA_IDS)){
				String ArrayDATA_IDS[] = DATA_IDS.split(",");//获取所有的reduApplyId
				for (String reduApplyId : ArrayDATA_IDS) {
					PageData info = ROIService.queryByReduApplyId(reduApplyId);
					info.put("contIdCard", CoverUtil.IdCard2(info.get("contIdCard").toString()));
					pathList.add(info);
					mv.addObject("DATA_IDS", DATA_IDS);
					mv.addObject("pathList", pathList);
				}
			}
		} catch (Exception e) {
			loger.error("罚息退免批量审核页面",e);
		}
		return mv;
	}
	/**
	 * 提交批量审核
	 * 
	 * @return
	 */
	@SysLog("罚息退免批量审核")
	@RequestMapping("auditBatchSave")
	@ResponseBody
	public ResultJSON auditBatchSave(String DATA_IDS,int auditStatus,String verifyRemark){
		loger.info("罚息减免批量审核:PenaltyInterestReductionController auditBatchSave:" + DATA_IDS);
		ResultJSON json = new ResultJSON();
		try {
			ROIService.updateAuditBatchSave(DATA_IDS,auditStatus,verifyRemark);
		} catch (Exception e) {
			loger.info("罚息退免批量审核:PenaltyInterestReductionController auditBatchSave:",e);
			json.setSuccess(false);
			json.setMsg(e.getMessage());
		}
		return json;
	}
	
	/**
	 * 罚息减免审核通过表
	 * 
	 * @param page
	 * @return
	 */
	@RequestMapping("auditPassList")
	public ModelAndView auditPassList(Page page){
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		
		//================高级查询===================
		String REDUAPPLYID = pd.getString("reduApplyId");
		if(null != REDUAPPLYID && !"".equals(REDUAPPLYID)){
			REDUAPPLYID = REDUAPPLYID.trim();
			pd.put("REDUAPPLYID", REDUAPPLYID);
		}
		String CALLLOANNID = pd.getString("callLoanNid");
		if(null != CALLLOANNID && !"".equals(CALLLOANNID)){
			CALLLOANNID = CALLLOANNID.trim();
			pd.put("CALLLOANNID", CALLLOANNID);
		}
		String REALNAME = pd.getString("realName");
		if(null != REALNAME && !"".equals(REALNAME)){
			REALNAME = REALNAME.trim();
			pd.put("REALNAME", REALNAME);
		}
		String BORROWNID = pd.getString("borrowNid");
		if(null != BORROWNID && !"".equals(BORROWNID)){
			BORROWNID = BORROWNID.trim();
			pd.put("BORROWNID", BORROWNID);
		}
		String REDUSTATUS = pd.getString("reduStatus");
		if(null != REDUSTATUS && !"".equals(REDUSTATUS)){
			REDUSTATUS = REDUSTATUS.trim();
			pd.put("REDUSTATUS", REDUSTATUS);
		}
		String RETURNSTATUS = pd.getString("returnStatus");
		if(null != RETURNSTATUS && !"".equals(RETURNSTATUS)){
			RETURNSTATUS = RETURNSTATUS.trim();
			pd.put("RETURNSTATUS", RETURNSTATUS);
		}
		//================高级查询===================
		page.setPd(UserUtils.setDataRole(pd,userService));//获取页面参数
		pd.put("STATUS", 1);
		page.setPd(pd);
		
		List<PageData>	list = ROIService.reduStatuslistPage(page);
		mv.setViewName("/penaltyInterestReduction/auditPassList");
		mv.addObject("pageList", list);
		mv.addObject("pd", pd);
		mv.addObject(Const.SESSION_QX,this.getHC());	//按钮权限
		return mv;
	}
	/**
	 * 罚息减免审核拒绝表
	 * 
	 * @param page
	 * @return
	 */
	@RequestMapping("auditRefuseList")
	public ModelAndView auditRefuseList(Page page){
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		
		//================高级查询===================
		String REDUAPPLYID = pd.getString("reduApplyId");
		if(null != REDUAPPLYID && !"".equals(REDUAPPLYID)){
			REDUAPPLYID = REDUAPPLYID.trim();
			pd.put("REDUAPPLYID", REDUAPPLYID);
		}
		String CALLLOANNID = pd.getString("callLoanNid");
		if(null != CALLLOANNID && !"".equals(CALLLOANNID)){
			CALLLOANNID = CALLLOANNID.trim();
			pd.put("CALLLOANNID", CALLLOANNID);
		}
		String REALNAME = pd.getString("realName");
		if(null != REALNAME && !"".equals(REALNAME)){
			REALNAME = REALNAME.trim();
			pd.put("REALNAME", REALNAME);
		}
		String BORROWNID = pd.getString("borrowNid");
		if(null != BORROWNID && !"".equals(BORROWNID)){
			BORROWNID = BORROWNID.trim();
			pd.put("BORROWNID", BORROWNID);
		}
		String REDUSTATUS = pd.getString("reduStatus");
		if(null != REDUSTATUS && !"".equals(REDUSTATUS)){
			REDUSTATUS = REDUSTATUS.trim();
			pd.put("REDUSTATUS", REDUSTATUS);
		}
		//================高级查询===================
		page.setPd(UserUtils.setDataRole(pd,userService));//获取页面参数
		pd.put("STATUS", 2);
		page.setPd(pd);
		
		List<PageData>	list = ROIService.reduStatuslistPage(page);
		Map<String,MaskTypeEnum> map = new HashMap<>();
        map.put("contIdCard",MaskTypeEnum.IDCARD2);
		CoverUtil.setListCover(list, map);
		mv.setViewName("/penaltyInterestReduction/auditRefuseList");
		mv.addObject("pageList", list);
		mv.addObject("pd", pd);
		mv.addObject(Const.SESSION_QX,this.getHC());	//按钮权限
		return mv;
	}
	
	
	 /* ===============================权限================================== */
    public Map<String, String> getHC(){
        Subject currentUser = SecurityUtils.getSubject();  //shiro管理的session
        Session session = currentUser.getSession();
        return (Map<String, String>)session.getAttribute(Const.SESSION_QX);
    }
	/* ===============================权限================================== */
}
